package com.rjh.base.socket;


import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class SocketSClient {

    public static void main(String[] args) throws IOException {
        //定义客户端socket
        Socket socket = new Socket(InetAddress.getLocalHost(),9999);
        //定于本地用于读取文件的输入流
        FileInputStream localFis = new FileInputStream("E:\\tmp\\test.png");
        //获取用socket返回对话的输出流
        OutputStream outputStream = socket.getOutputStream();
        //读取本地文件输入流 通过 socket 上传至服务端
        System.out.println("开始上传图片");
        byte[] bytes = new byte[1024];
        int readLen;
        while((readLen= localFis.read(bytes))!=-1){
            outputStream.write(bytes,0,readLen);
        }
        //上传完成返回结束标识
        socket.shutdownOutput();
        System.out.println("上传图片完成");

        //从socket对象中回去socket输入流 通过转换流  最终使用包装流用于读取服务端发送的消息
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        //获取服务端发送的消息
        String readLine = bufferedReader.readLine();
        System.out.println(readLine);

        //关闭资源
        bufferedReader.close();
        outputStream.close();
        localFis.close();
        socket.close();
    }
}
